package com.wentao.thisService.service.impl;

import com.wentao.thisFacade.common.UserInfo;
import com.wentao.thisFacade.common.enums.business.RoleCodeEnum;
import com.wentao.thisFacade.common.enums.business.ValidStatusEnum;
import com.wentao.thisService.common.redis.RedisExecutor;
import com.wentao.thisService.mapper.CompanyCustomDao;
import com.wentao.thisService.mapper.UserAuthorityDao;
import com.wentao.thisService.mapper.UserDao;
import com.wentao.thisService.model.CompanyCustomEntity;
import com.wentao.thisService.model.UserAuthorityEntity;
import com.wentao.thisService.model.UserEntity;
import com.wentao.thisService.service.CacheUserService;
import com.wentao.thisService.service.CommunalService;
import com.wentao.thisService.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 10:01
 * @Description: 用户相关service实现
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisExecutor redisExecutor;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserAuthorityDao userAuthorityDao;

    @Autowired
    private CompanyCustomDao companyCustomDao;

    @Autowired
    private CommunalService communalService;

    @Autowired
    private CacheUserService cacheUserService;

    /**
     * @see UserService#initUserInfoToRedis()
     */
    @Override
    public Map<String, UserInfo> initUserInfoToRedis() throws Exception {
        // 用户角色信息
        UserAuthorityEntity searchUserRole = new UserAuthorityEntity();
        searchUserRole.setStatus(ValidStatusEnum.VALID.getCode());
        List<UserAuthorityEntity> userAuthorityEntityList = userAuthorityDao.Search(searchUserRole);
        Map<String, String> userCodeRoleCodeMap = new HashMap<>();
        if (null != userAuthorityEntityList && userAuthorityEntityList.size() > 0) {
            for (UserAuthorityEntity userAuthorityEntity : userAuthorityEntityList) {
                if (null != userAuthorityEntity) {
                    userCodeRoleCodeMap.put(userAuthorityEntity.getUserCode(), userAuthorityEntity.getRoleCode());
                }
            }
        }

        // 酒店商家
        CompanyCustomEntity searchCompanyCustom = new CompanyCustomEntity();
        searchCompanyCustom.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompanyCustom);
        Map<Long, CompanyCustomEntity> userIdCompanyMap = new HashMap<>();
        if (null != companyCustomEntityList && companyCustomEntityList.size() > 0) {
            for (CompanyCustomEntity companyCustomEntity : companyCustomEntityList) {
                if (null != companyCustomEntity) {
                    userIdCompanyMap.put(companyCustomEntity.getUserId(), companyCustomEntity);
                }
            }
        }

        // 用户信息
        UserEntity searchUser = new UserEntity();
        searchUser.setStatus(ValidStatusEnum.VALID.getCode());
        List<UserEntity> userEntityList = userDao.Search(searchUser);

        // 酒店入驻步骤信息
        Map<String, String> companyCodeReviewStatusMap = communalService.hotelReviewStatus();

        // 用户信息map
        Map<String, UserInfo> userIdInfoMap = new HashMap<>();
        if (null != userEntityList && userEntityList.size() > 0) {
            UserInfo userInfo;
            for (UserEntity userEntity : userEntityList) {
                if (null != userEntity) {
                    userInfo = new UserInfo();

                    userInfo.setUserId(userEntity.getId());
                    userInfo.setPassword(userEntity.getPassword());
                    userInfo.setUserName(userEntity.getUserName());
                    userInfo.setUserCode(userEntity.getUserCode());
                    userInfo.setMobile(userEntity.getMobile());
                    userInfo.setLockStatus(userEntity.getLockStatus());
                    userInfo.setRoleCode(userCodeRoleCodeMap.get(userEntity.getUserCode()));
                    if (RoleCodeEnum.HOTEL.getCode().equals(userInfo.getRoleCode())) {
                        CompanyCustomEntity companyCustom = userIdCompanyMap.get(userInfo.getUserId());
                        userInfo.setCompanyCode(companyCustom.getCompanyCode());
                        userInfo.setCompanyName(companyCustom.getCompanyName());
                        userInfo.setCompanyType(companyCustom.getCompanyType());
                        userInfo.setCompanyContact(companyCustom.getCompanyContact());
                        userInfo.setApproveUser(companyCustom.getApproveUser());
                        communalService.hotelReviewStatus(companyCustom.getCompanyCode());
                        userInfo.setRegistSteps(companyCodeReviewStatusMap.get(companyCustom.getCompanyCode()));
                        userInfo.setPartnerId(companyCustom.getPartnerId());
                        userInfo.setRegistTime(companyCustom.getRegistTime());
                        userInfo.setLongitude(companyCustom.getLongitude());
                        userInfo.setLatitude(companyCustom.getLatitude());
                        userInfo.setAddress(companyCustom.getAddress());
                        userInfo.setLocation(companyCustom.getLocation());
                    }

                    userIdInfoMap.put(String.valueOf(userInfo.getUserId()), userInfo);

                    // TODO: 2020/3/27 测试能否放入缓存
                    cacheUserService.putToUserInfoCache(userInfo);
                }
            }
        }

        return userIdInfoMap;
    }
}
